package com.xbongbong.saas.model;

import com.xbongbong.paas.domain.entity.StageLogEntity;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.saas.domain.entity.OpportunityStageLogEntity;
import org.apache.ibatis.annotations.Param;

import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * @author haibin.zhang
 * @version v1.0
 * @date 2019/3/10 16:48
 * @since v1.0
 */
public interface OpportunityStageLogModel {

    /**
     * 计算某些阶段的某些机会，在这些阶段的停留时长
     * @param corpid 公司id
     * @param opportunityIdIn 要查询的机会ids
     * @param stageCodeIn 要查询的阶段codes
     * @return java.util.Map<java.lang.String,java.lang.Double> 返回map，内部结构为：阶段code--该批机会在该阶段的平均停留时长
     * @author zcp
     * @date 2019/3/10 17:18
     * @since v1.0
     * @version v1.0
     */
    Map<String, Double> getAverageStayTime(String corpid, Set<Long> opportunityIdIn, List<String> stageCodeIn);

    /**
     * 获取当前阶段记录
     * @param opportunityId
     * @param corpid
     * @return
     */
    OpportunityStageLogEntity getNowStageLog(Long opportunityId, String corpid, Long formId);

    /**
     * 通过阶段id获取当前记录
     *
     * @param opportunityId
     * @param corpid
     * @param stageId
     * @return
     */
    OpportunityStageLogEntity getStageLogByStageId(Long opportunityId, String corpid, Long stageId);

    /**
     * 当前阶段的停留时长
     *
     * @param corpid 公司id
     * @param opportunityId 机会id
     * @return
     * @throws
     * @author hongxiao
     * @date: 2019-04-16 15:49
     * @since v1.0
     * @version v1.0
     */
    String getAverageStayTime(String corpid, Long opportunityId, Long formId) throws XbbException;

    Integer getEntitysCount(Map<String, Object> param);

    List<OpportunityStageLogEntity> findEntitys(Map<String,Object> param);

    void save(OpportunityStageLogEntity opportunityStageLogEntity);

    /**
     * 批量插入
     * @author 吴峰
     * @date 2019/3/20 13:43
     * @param opportunityStageLogList
     * @return java.util.List<com.xbongbong.saas.domain.entity.OpportunityStageLogEntity>
     */
    List<OpportunityStageLogEntity> insertBatch(List<OpportunityStageLogEntity> opportunityStageLogList);
    Integer deleteByStageProcessIdAndDataIdIn(List<Long> dataIdIn, String corpid, Long formId, Long stageProcessId, Long stageId);
    /**
     * 按照最近一次跳转的跳入时间，在固定时间范围内的销售机会id
     * @param param
     * @return
     */
    List<Long> getLastStageLogIdsByIntoTime(@Param("param")Map<String ,Object> param);

    /**
     * 批量编辑
     * @author 吴峰
     * @date 2019/3/20 13:43
     * @param opportunityStageLogList
     * @param corpid 公司id
     * @return java.util.List<com.xbongbong.saas.domain.entity.OpportunityStageLogEntity>
     */
    List<OpportunityStageLogEntity> updateBatch(List<OpportunityStageLogEntity> opportunityStageLogList, String corpid);

    List<OpportunityStageLogEntity> updateBatch4Script(List<OpportunityStageLogEntity> opportunityStageLogList);

    OpportunityStageLogEntity getByKey(Long id, String corpid);

    List<OpportunityStageLogEntity> findStageLogWithoutStage();

    /**
     * 批量删除
     * @param stageLogEntityList
     * @param corpid
     * @return
     */
    Integer deleteBatch(List<StageLogEntity> stageLogEntityList, String corpid);

    /**
     * 获取某个阶段流程下是否存在关联的机会数据
     * 不关注机会数据是否已经在主表被删除
     *
     * @param corpid 公司id
     * @param formId 表单id
     * @param stageProcessId 阶段流程id
     * @return true/false 存在/不存在
     */
    boolean existOpportunityByStageProcessId(String corpid, Long formId, Long stageProcessId);

    /**
     * 根据dataId 查找当前数据所处阶段
     * @param corpid
     * @param formId
     * @param dataId
     * @return
     */
    OpportunityStageLogEntity getIsNowStage(String corpid, Long formId, Long dataId);

    Long getAvgStayTime(String corpid, Long stageProcessId, Long stageId);

    List<Long> findUsedStageProcessId(String corpid, Long formId, List<Long> stageProcessIdIn);
}
